package com.mcr.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcr.reggie.common.R;
import com.mcr.reggie.entity.Employee;
import com.mcr.reggie.service.EmployeeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping(value = "/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     *
     *
     *  *  *  *  *  *  *  *  *  *  *  *  后台*  *  *  *  *  *  *  *  *  *  *  *  *  *  *
     *
     *
     */
    @ApiOperation(value = "后台登录")
    @PostMapping(value = "/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        Employee emp=employeeService.loginVerify(employee);
        if(emp==null){
            return R.error("登陆失败,用户名或密码错误");
        }
        if(emp.getStatus()==0){
            return R.error("用户状态已禁用");
        }
        request.getSession().setAttribute("emp",emp.getId());
        return R.success(emp);
    }
    @ApiOperation(value = "后台退出登录")
    @PostMapping(value = "/logout")
    public R<Employee> logout(HttpServletRequest request){
        request.getSession().removeAttribute("emp");
        return R.success(null);
    }
    @ApiOperation(value = "新增员工")
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
        employeeService.saveEmp(employee);
        return R.success("保存成功！！");
    }
    @ApiOperation(value = "员工信息查询")
    @GetMapping(value = "/page")
    public R<Page> page(@RequestParam(value = "page",required = true) int page,
                        @RequestParam(value = "pageSize",required = true) int pageSize,
                        @RequestParam(value = "name",required = false) String name){
        Page<Employee> employeePage=employeeService.getPage(page,pageSize,name);
        return R.success(employeePage);
    }
    @ApiOperation(value = "根据id修改员工信息")
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        boolean t=employeeService.updateEmp(employee);
        if(t){
            return R.success("账号状态更改成功！");
        }
        return R.error("不是超级管理员没有权限进行更改");
    }
    @ApiOperation(value = "根据id获取员工信息")
    @GetMapping(value = "/{id}")
    public R<Employee> getById(@PathVariable("id") Long id){
        Employee employee = employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("无此员工信息");
    }
}
